const Router = require('koa-router');
const { success, fail } = require('../utils/res');
const router = new Router();

router.post('/login', async (ctx) => {
  const { body } = ctx.request;
  console.log('/login', body);
  if (!body.userID) {
    ctx.response.body = fail('用户异常！');
  } else {
    if (global.userMap[body.userID]) {
      ctx.response.body = success({ code: 1, message: `${body.userID} 用户重复登录！` });
      return;
    }
    global.userMap[body.userID];
    ctx.response.body = success({ code: 0, message: '登录成功！' });
  }
});

router.post('/logout', async (ctx) => {
  const { body } = ctx.request;
  if (!body.userID) {
    ctx.response.body = fail('用户异常！');
  } else {
    if (!global.userMap[body.userID]) {
      ctx.response.body = success({ code: 1, message: `${body.userID} 用户不存在` });
      return;
    }
    delete global.userMap[body.userID];
    ctx.response.body = success({ code: 0, message: '登出成功！' });
  }
});

module.exports = router;
